import { commands, Uri, window } from 'vscode';
import { CommandConst } from '../../constants';
import { manage } from '../../preview-manager';

/**
 * 模板文件预览
 *
 * @author chitanda
 * @date 2022-01-18 11:01:31
 * @export
 * @class TemplatePreviewCommand
 */
export class TemplatePreviewCommand {
  constructor() {
    commands.registerCommand(CommandConst.TEMPLATE_PREVIEW, this.execute, this);
  }

  /**
   * 预览的模板文件
   *
   * @author chitanda
   * @date 2022-02-10 17:02:57
   * @param {Uri} uri
   * @return {*}  {Promise<void>}
   */
  async execute(uri: Uri): Promise<void> {
    if (uri) {
      try {
        await manage.preview(uri);
      } catch (err: any) {
        window.showErrorMessage(`预览失败：${err.message}`);
      }
    } else {
      window.showErrorMessage(`预览模板失败，未指定模板文件`);
    }
  }
}
